﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;

using Revis.VClinicNet.Core.Domain.Credentials;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class LoginController : Infrastructure.ControllerBase
    {
        public static readonly IDictionary<WorkerUserRole, string> HomePageUrl = new Dictionary<WorkerUserRole, string>() {
            {WorkerUserRole.Administrator, "/WorkerUser"},
            {WorkerUserRole.Recepcionist, "/Patient/IndexR"},
            {WorkerUserRole.Doctor, "/Visit/Today"}
        };

        public ActionResult Index()
        {
            //return Login("admin", "admin", false);
            return View();
        }

        [HttpPost]
        public ActionResult Login(string login, string password, bool remember)
        {
            if (ModelState.IsValid) {
                if (Membership.Provider.ValidateUser(login, password) == true) {
                    FormsAuthentication.SetAuthCookie(login, remember);
                    ClinicUser user = (ClinicUser)Membership.Provider.GetUser(login, true);
                    this.StoreUser(user);
                    return Redirect(HomePageUrl[user.Role]);
                }
            }
            return Redirect("Index");
        }

        [HttpGet]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            this.ForgetUser();
            return Redirect("/Login/Index");
        }
    }
}
